using System;
using System.Collections.Generic;
using System.Text;
using Barrage.GUI;
using Barrage.GUI.Elements;
using SlimDX.Direct3D9;
using Barrage.Global;

namespace Spacedash.Game.Subgames
{
    static class ConfigurationElements
    {
        public const string ExitButton = "EXIT_BUTTON";
        public const string FullScreenCheckbox = "FULLSCREEN_CHECK";
        public const string AntialiasCheckbox = "ANTIALIAS_CHECK";
        public const string ResolutionSelector = "RESOLUTION_SELECT";
    }
    class ConfigurationSection : GUISection
    {
        public override int Id
        {
            get { return (int)GameState.Configuration; }
        }

        public ConfigurationSection(SpacedashGame parent)
            : base("Configuration", parent)
        {
            RegisterExitEvent();
            FillResolutions();
        }

        void FillResolutions()
        {
            GUISelector selector = ((GUISelector)Layout.Elements[ConfigurationElements.ResolutionSelector]);
            for (int i=0;i<Engine.DisplayModes.Count;i++)
                selector.addOption(i,GetDisplayModeText(i));
            selector.CurrentOption = Engine.CurrentDisplayModeIndex;
        }

        string GetDisplayModeText(int index)
        {
            DisplayMode dm = Engine.DisplayModes[index];

            return dm.Width.ToString() + "x" + dm.Height;
        }

        void RegisterExitEvent()
        {
            try
            {
                ((GUIButton)Layout.Elements[ConfigurationElements.ExitButton]).OnInteraction += new GUIButtonHandler(ExitButtonClick);
            }
            catch (Exception)
            {
            }
        }

        public void ExitButtonClick(string buttonId)
        {
            bool fullscreen = ((GUICheckbox)Layout.Elements[ConfigurationElements.FullScreenCheckbox]).Checked;
            Engine.Config[EngineConfig.FULLSCREEN] = XmlHelper.GetBoolValue(fullscreen);
            Engine.Config[EngineConfig.RESOLUTION] = ((GUISelector)Layout.Elements[ConfigurationElements.ResolutionSelector]).CurrentOptionValue;
            bool antialias = ((GUICheckbox)Layout.Elements[ConfigurationElements.AntialiasCheckbox]).Checked;
            Engine.Config[GameConfig.ANTIALIAS] = XmlHelper.GetBoolValue(antialias);
            Engine.Config.Save(Engine.CONFIG_FILE);
            Parent.NextGameSectionId = (int)GameState.MainMenu;
        }
    }
}
